

export class TimeUtil{
//@ts-ignore
    private static PrefixInteger(num, length):string{

        return  (Array(length).join('0') + num).slice(-length);
    }

    //将一个时间数转换成"hh:mm:ss"格式
    public static GetTimeStringHMS(timeInt : number):string {

        if(timeInt <= 0){
            return "00:00:00";
        }else{

            let hour:number = Math.floor(timeInt / (60 * 60));
            let hourstr = this.PrefixInteger(hour, 2);

            let minnute:number = Math.floor(timeInt/ 60) % 60;
            let minutestr:string = this.PrefixInteger(minnute, 2);

            let second:number = Math.floor(timeInt) % 60;
            let secondstr:string = this.PrefixInteger(second,2);

            return `${hourstr}:${minutestr}:${secondstr}`;
        }
        
    }

    //将一个时间数转换成"hh:mm"格式
    public static GetTimeStringHM(timeInt:number):string{

        if(timeInt <= 0){
            return "00:00:00";
        }else{
            let hour:number = Math.floor(timeInt / (60 * 60));
            let hourstr = this.PrefixInteger(hour, 2);

            let minnute:number = Math.floor(timeInt/ 60) % 60;
            let minutestr:string = this.PrefixInteger(minnute, 2);

            return `${hourstr}:${minutestr}`;
        }
    }

    //将一个时间数转换成"00"分格式
    public static GetTimeMinuteString(timeInt:number):string{

        if(timeInt <= 0){
            return "00:00:00";
        }else{
        
            let minnute:number = Math.floor(timeInt/ 60) % 60;
            let minutestr:string = this.PrefixInteger(minnute, 2);

            return `${minutestr}`;
        }
    }

    //将一个时间数转换成"00“秒格式
    public static GetTimeSecondString(timeInt:number):string{

        if(timeInt <= 0){
            return "00:00:00";
        }else{
            let second:number = Math.floor(timeInt) % 60;
            let secondstr:string = this.PrefixInteger(second,2);

            return `${secondstr}`;
        }
    }

    //获取本月1号是星期几
    public static GetWeekOfMonthFirstDay(time:number):number{

        let date = new Date(time);
        date.setDate(1);

        return date.getDay();
    }

    //判断是否为闰年
    public static IsLeapYear(year:number)
    {
        if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ){
            return true;
        }
        return false;
    }

    //每个月对应的天数
    private static  months:Array<number> = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    private static getMonthDays_(year:number, month:number):number{
        if(month == 2){
            if(this.IsLeapYear(year))
                return 29;
            else{
                return 28;
            }
        }else{
            return this.months[month];
        }
    }

    public static GetMonthDays(time:number):number {

        let t = new Date(time);

        return this.getMonthDays_(t.getFullYear(), t.getMonth());
    }


    public static async Sleep(ms:number){
        return new Promise((resolve) =>{
            setTimeout(()=>{
                resolve('');
            }, ms);
        });
    }

}